#!/usr/bin/env python

import string
import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

class Mail(object):
    '''Mail class

    @author Julot <andy.julot@gmail.com>
    @since Saturday, September 22, 2012 09:28 AM'''

    def __init__(self, subject, message, recipient, sender):
        self.subject= subject
        self.message = message
        self.recipient = recipient
        self.sender = sender

    def sendTelkom(self):
        subject = self.subject
        s = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s"
        data = (sender, self.recipient, subject, self.message)
        body = s % data
        server = smtplib.SMTP('smtp.telkom.net')
        server.sendmail(self.sender, [self.recipient], body)
        server.quit()
        return True

    def sendGmail(self, username='', password=''):
        msg = MIMEMultipart()

        msg['From'] = self.sender
        msg['To'] = self.recipient
        msg['Subject'] = self.subject

        msg.attach(MIMEText(self.message))

##        part = MIMEBase('application', 'octet-stream')
##        part.set_payload(open(attach, 'rb').read())
##        Encoders.encode_base64(part)
##        part.add_header('Content-Disposition',
##               'attachment; filename="%s"' % os.path.basename(attach))
##        msg.attach(part)

        mailServer = smtplib.SMTP("smtp.gmail.com", 587)
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        mailServer.login(username, self.password)
        mailServer.sendmail(username, self.recipient, msg.as_string())
        # Should be mailServer.quit(), but that crashes...
        mailServer.close()
        return True
